﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Diagnostics.Contracts;

using E = System.Linq.Expressions.Expression;

namespace ReactiveLang.ExpressionConverter.Util
{
    class ExpressionReplacer: ExpressionVisitor
    {
        Func<E, E> _selector;

        public E Replace(E expression, Func<E, E> selector)
        {
            Contract.Requires(expression != null && selector != null);
            _selector = selector;
            return Visit(expression);
        }

        public override E Visit(E node)
        {
            var result = _selector(node);
            if (result != ExpressionUtil.Nothing)
                return result;
            return base.Visit(node);
        }
    }
}
